package order

import (
	"APi/client"
	"fmt"
	"github.com/gin-gonic/gin"
	order "order_srv/proto"
	"strconv"
	user "user_srv/proto"
)

func CallBack(c *gin.Context) {
	fmt.Println("ok  res ")
	outTradeNo := c.Request.FormValue("out_trade_no")
	tradeStatus := c.Request.FormValue("trade_status")
	buyerPayAmount, _ := strconv.Atoi(c.Request.FormValue("buyer_pay_amount"))
	var status int
	switch tradeStatus {
	case "WAIT_BUYER_PAY":
		status = 0 //交易创建，等待买家付款。
	case "TRADE_CLOSED":
		status = 1 //未付款交易超时关闭，或支付完成后全额退款。
	case "TRADE_SUCCESS":
		status = 2 //交易支付成功。
	case "TRADE_FINISHED":
		status = 3 //交易结束，不可退款。
	}
	res, _ := client.UpdateOrderStatus(c, &order.UpdateOrderStatusRequest{
		OrderId: outTradeNo,
		Status:  int64(status),
	})

	client.UserIntegralChange(c, &user.UserIntegralChangeRequest{
		UserId:             res.UserId,
		ControlsType:       2,
		OperationIntegral:  int64(buyerPayAmount),
		OperationalReasons: "购物赠送积分",
	})
}
